//
//  SEGAutoHeightImageViewModel.m
//  Commom
//
//  Created by Clavis on 2019/7/29.
//

#import "SEGAutoHeightImageViewModel.h"
#import <Commom/SEGColorManager.h>
#import <Commom/SEGLayoutMacro.h>

@implementation SEGAutoHeightImageViewModel
-(instancetype)init{
    if (self == [super init]) {
        _pic = [NSString stringWithFormat:@"a_%zd_%zd.png",LYScreenWidth(),kAutoHeightImageViewHeight];
        _pictureWidth = LYScreenWidth();
    }
    return self;
}
@end

@implementation SEGAutoHeightImageViewLayout
- (instancetype)initWithData:(SEGAutoHeightImageViewModel *)data type:(SEGEnumAutoHeightImageViewType)type{
    if (self == [super init]) {
        _data = data;
        _type = type;
    }
    [self segAutoHeightImageViewLayout];
    return self;
}

-(void)segAutoHeightImageViewLayout{
    //高度固定
    _totalHeight = kAutoHeightImageViewHeight;
    
    //高度不固定
    //_totalHeight = 0;
    
    if (!_data) return;
    
    [self layout];
}

//数据处理
-(void)layout{
    [self segCalcHeight];
}

/**
 * 动态计算高度
 * @author Sam
 */
-(void)segCalcHeight{
    /*
     1 新图：
     1.1 最大比例：3:4
     1.2 最小比例：2:1
     2 老图
     2.1 默认：16:9
     */
    
    CGFloat newMaxScale = 1.333;// 3:4
    CGFloat newMinScale = 0.5;// 2:1
    
    NSArray *data = [_data.pic componentsSeparatedByString:@"_"];
    
    if (data.count == 3) {//新图
        CGFloat picWith = [data[1] floatValue];
        CGFloat picHeight = [[data[2] componentsSeparatedByString:@"."].firstObject floatValue];
        CGFloat scale = picHeight/picWith;
        scale = scale > newMaxScale ? newMaxScale : scale;
        scale = scale < newMinScale ? newMinScale : scale;
        _totalHeight = _data.pictureWidth * scale;
    }else{//旧图
        _totalHeight = _data.pictureWidth * 0.5625;//16:9
    }
}
@end
